package com.yuelan.apartment.controller;

import com.yuelan.apartment.domain.TenantInfo;
import com.yuelan.apartment.domain.dto.TenantDto;
import com.yuelan.apartment.service.TenantService;
import com.yuelan.common.core.utils.poi.ExcelUtil;
import com.yuelan.common.core.web.controller.BaseController;
import com.yuelan.common.core.web.domain.AjaxResult;
import com.yuelan.common.core.web.page.TableDataInfo;
import com.yuelan.common.log.annotation.Log;
import com.yuelan.common.log.enums.BusinessType;
import com.yuelan.common.security.annotation.Logical;
import com.yuelan.common.security.annotation.RequiresPermissions;
import com.yuelan.common.security.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * @description: 租户管理
 * @Author: ZhaoYi
 * @date: 24-5-23 17:01
 */

@RestController
@RequestMapping(value = "/tenant")
public class TenantController extends BaseController {

    @Resource
    private TenantService tenantInfoService;


    @PostMapping("/export")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "导出房客信息列表", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, TenantInfo tenantInfo) {
        List<TenantInfo> list = tenantInfoService.pageList(tenantInfo);
        ExcelUtil<TenantInfo> util = new ExcelUtil<>(TenantInfo.class);
        util.exportExcel(response, list, "房客信息数据");
    }


    @PostMapping("/add")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "租户信息录入", businessType = BusinessType.INSERT)
    public AjaxResult addTenant(@RequestBody @Valid TenantDto tenantDto){
        return toAjax(tenantInfoService.add(tenantDto));
    }


    @DeleteMapping("/delete")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "租户清除", businessType = BusinessType.DELETE)
    public AjaxResult remove(Long id){
        return toAjax(tenantInfoService.delete(id));
    }


    @DeleteMapping("/{ids}")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "房客信息", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tenantInfoService.deleteTenantByIds(ids));
    }


    @PutMapping("/edit")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "租户信息更新", businessType = BusinessType.UPDATE)
    public AjaxResult updateTenant(@RequestBody @Valid TenantInfo tenantInfo) {
        return toAjax(tenantInfoService.update(tenantInfo));
    }


    @GetMapping("/load")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "租户信息查看", businessType = BusinessType.QUERY)
    public AjaxResult load(Long id) {
        TenantInfo load = tenantInfoService.load(id);
        return success(load);
    }


    @GetMapping("/pageList")
    @RequiresRoles(value = {"admin", "common"}, logical = Logical.OR)
    @Log(title = "查询所有租户-分页查询", businessType = BusinessType.QUERY)
    public TableDataInfo pageList(TenantInfo tenantInfo) {
        return tenantInfoService.page(tenantInfo);
    }

}
